<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class IndexController extends Controller
{
	// 前台首页控制器
    //  显示主页
    public function index()
    {
    	return view('home.index.index');
    }
    /*
	根据pid获取当前类下的所有子分类
    */
    public static function getCateByPid($pid)
    {
    	//获取$pid下的所有分类
    	$res = DB::table('shop_type')->where('pid',$pid)->get();
    	$data = [];
    	foreach($res as $k=>$v){
    		$v->sub = self::getCateByPid($v->tid);
    		$data[] = $v;
    	}
    	return $data;
    }

    /*
        分类
    */

    public static function getHeader()
    {
    	$cates = self::getCateByPid(0);
    	//解析模板
    	return view('home.layout.header',['cates'=>$cates]);
    }

    /*
        轮播
    */

    public static function getLunbo()
    {

        $lunbos = DB::table('shop_lunbo')->get();
        foreach($lunbos as $k=>$v){
            if($k == 0){
                foreach($v as $kk=>$vv){
                    $data[$kk] = $vv;
                }
            }
        }

        return view('home.index.lunbo',['lunbos'=>$lunbos,'data'=>$data]);
    }

    /*
        包含主页尾
    */
    public static function getButtom()
    {
        $links = DB::table('shop_links')->get();
        //解析模板
        return view('home.layout.buttom',['links'=>$links]);
    }

    /*
        包含主页商品列表
    */
    public static function getList()
    {
        $list = DB::table('shop_brand')->where(['bsize'=>1,'status'=>'上架'])->get();
        $lists = DB::table('shop_brand')->where(['bsize'=>2,'status'=>'上架'])->get();
        //解析模板
        return view('home.index.list',['list'=>$list,'lists'=>$lists]);
    } 

    /*
        折扣首页
    */
    public function getDiscout()
    {
        
        //解析模板
        return view('home.discout.index');
    }

    /*
        折扣ajax
    */
    public function getPubu(Request $request)
    {
        $p = $request->input('p');
        $list = DB::table('shop_brand')->where('bsize','3')->get();
        //获取总记录数
        $res = DB::table('shop_brand')->where(['bsize'=>3,'status'=>'上架'])->skip($p)->take(1)->get();
        
        // $num = $res[0]->num;
        // if($request->input('p') >= $num){
        //  echo 0;
        // }
        echo json_encode($res);
    }

    /*  
        排序
    */
    public function getPaixu(Request $request)
    {
        $id = $request->input('id');
        if($id == 1){
            $times = time() + 86400;

            $brand = DB::select('select * from shop_brand where bsize = 3 and status = ? and btime <= ?',['上架',$times]);

            // $brand = DB::table('shop_brand')->where('bsize','3')->get();
            return view('/home/discout/paixu',['brand'=>$brand,'id'=>$id]);
        }
        if($id == 2){
            $times = time() - 3600;
            $brand = DB::select('select * from shop_brand where bsize = 3 and status = ? and regtime > ?',['上架',$times]);
            
            return view('/home/discout/paixu',['brand'=>$brand,'id'=>$id]);
        }
    }




}
